package org.bitmap.mm.util;

import java.lang.ref.SoftReference;

import android.os.Message;

class LoveBitmapTasks implements Runnable {
	public static final byte MODE_SoftReference = 1;
	public static final byte MODE_DEFAULT = 0;
	public  final ImageData queue;
	private final LoveBitmapManager loveBitmapManager;
	private boolean cancel = false;
	SoftReference<LoveBitmapHandler> handlerReference;
	LoveBitmapHandler loveBitmapHandler;
	private byte mMode;
	public LoveBitmapTasks(LoveBitmapManager love, LoveBitmapHandler handler,byte mode,
			ImageData info) {
		mMode = mode;
		if(mMode==MODE_DEFAULT){
			loveBitmapHandler = handler;
		}else if(mMode==MODE_SoftReference){
			handlerReference = new SoftReference<LoveBitmapHandler>(handler);
		}
		loveBitmapManager = love;
		queue = info;
	}
	@Override
	public void run() {
		try {
			if (!cancel) {
				ImageData info = queue;
				if (!loveBitmapManager.isExitTasksEarly()) {
						HttpLove.downloadBitmapToFile(
								loveBitmapManager.getContext(),
								String.valueOf(info));
						LoveBitmapHandler handler =  null ;
						if(mMode==MODE_DEFAULT){
							handler = 	loveBitmapHandler;
						}else if(mMode==MODE_SoftReference){
							handler = 	handlerReference.get();
						}
						if(handler==null){
							handler = loveBitmapManager.getDefaultLoveBitmapHandler();
						}
						if(handler!=null){
						Message msg = handler.obtainMessage(LoveBitmapHandler.STATE_BITMAP_SET);
						msg.obj = info;
						handler.sendMessage(msg);
						}
						
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			System.gc();
		}
		System.gc();

	}
	public boolean isCancel() {
		return cancel;
	}

	public void setCancel(boolean cancel) {
		this.cancel = cancel;
	}

}
